package topofstack.networkserverutils.fragments;

import java.util.Timer;
import java.util.TimerTask;

import topofstack.networkserverutils.R;
import topofstack.networkserverutils.bean.MailInfo;
import topofstack.networkserverutils.mail.ReceiveMailPOP;
import topofstack.networkserverutils.mail.SendMailSSL;
import topofstack.networkserverutils.util.Constants;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class DeliveryTime extends Fragment{
	private EditText smtpServerField;
	private EditText smtpPortField;
	private EditText popServerField;
	private EditText popPortField;
	private EditText usernameField;
	private EditText passwordField;
	private Button sendMail;
	
	private ProgressDialog dialog;
	private Timer timer;
	boolean foundMail;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View convertView = inflater.inflate(R.layout.test_mail, container, false);
		
		smtpServerField = (EditText) convertView.findViewById(R.id.smtp_server);
		smtpPortField = (EditText) convertView.findViewById(R.id.smtp_port);
		popServerField = (EditText) convertView.findViewById(R.id.pop_server);
		popPortField = (EditText) convertView.findViewById(R.id.pop_port);
		usernameField = (EditText) convertView.findViewById(R.id.test_mail_username);
		passwordField = (EditText) convertView.findViewById(R.id.test_mail_password);
		sendMail = (Button) convertView.findViewById(R.id.test_mail_submit);
		
		sendMail.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				new SendMail().execute(new Void[0]);
				
				dialog = new ProgressDialog(getActivity());
				dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
				dialog.setMessage("Sending Mail..");
				dialog.setIndeterminate(true);
				dialog.show();
				foundMail = false;
			}
		});
		
		return convertView;
	}
	
	@Override
	public void onPause() {
		if(timer != null)
			timer.cancel();
	}
	
	class SendMail extends AsyncTask<Void, Void, Void> {
		
		boolean mailSent;

		@Override
		protected Void doInBackground(Void... arg0) {
			String host = smtpServerField.getText().toString();
			String port = smtpPortField.getText().toString();
			String email = usernameField.getText().toString();
			String password = passwordField.getText().toString();
			
			SendMailSSL sendMailSSL = new SendMailSSL();
			mailSent = sendMailSSL.send(host, port, email, password);
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			dialog.cancel();
			if(mailSent) {
				timer = new Timer();
				timer.scheduleAtFixedRate(new Task(), 0, 5000);
				
				dialog = new ProgressDialog(getActivity());
				dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
				dialog.setMessage("Fetching Mail..");
				dialog.setIndeterminate(true);
				dialog.show();
			}
			else {
				Toast.makeText(getActivity(), "Error Sending Mail", Toast.LENGTH_SHORT)
					.show();
			}
		}
	}
	
	class ReceiveMail extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... arg0) {
			String host = popServerField.getText().toString();
			String port = popPortField.getText().toString();
			String email = usernameField.getText().toString();
			String password = passwordField.getText().toString();
			
			ReceiveMailPOP receiveMail = 
					new ReceiveMailPOP(host, port, email, password);
			return receiveMail.get();
		}
		
		@Override
		protected void onPostExecute(String result) {
			
			if(foundMail)
				return;
			if(result == null) {
				Toast.makeText(getActivity(), "Error Receiving Mail", Toast.LENGTH_SHORT)
					.show();
				timer.cancel();
				dialog.cancel();
				foundMail = true;
			}
			else {
				if(!result.contains("-1")) {
					dialog.cancel();
					timer.cancel();
					AlertDialog.Builder resultDialog = 
							new AlertDialog.Builder(getActivity());
					resultDialog.setMessage(result).setTitle("Message Header");
					resultDialog.show();
					foundMail = true;
				}
			}
		}
	}
	
	class Task extends TimerTask  {

		@Override
		public void run() {
			Log.d("Timer", "Trying Again");
			new ReceiveMail().execute(new Void[0]);
		}
	}
}
